﻿using BMS.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ApiServer.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BaseController<T> : ControllerBase where T : class
    {
        private readonly IBaseService<T> baseService;
        public BaseController(IBaseService<T> baseService)
        {
            this.baseService = baseService;
        }
        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file">图片文件</param>
        /// <returns></returns>
        [HttpPost("upload")]
        [ApiExplorerSettings(GroupName = "v2")]
        public IActionResult UpLoad(IFormFile file)
        {
            return Ok(baseService.UpLoad(file));
        }

        #region 基础的增删改查
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">实体信息</param>
        [HttpPost("base")]
        [ApiExplorerSettings(GroupName = "v2")]
        public async Task<IActionResult> Insert(T entity)
        {
            return Ok(await baseService.Insert(entity));
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities">实体信息集合</param>
        [HttpPost("batch")]
        [ApiExplorerSettings(GroupName = "v2")]
        public async Task<IActionResult> InsertBatch(List<T> entities)
        {
            return Ok(await baseService.InsertBatch(entities));
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">实体信息</param>
        [HttpPut("base")]
        [ApiExplorerSettings(GroupName = "v2")]
        public async Task<IActionResult> Update(T entity)
        {
            return Ok(await baseService.Update(entity));
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="entities">实体信息集合</param>
        [HttpPut("batch")]
        [ApiExplorerSettings(GroupName = "v2")]
        public async Task<IActionResult> UpdateBatch(List<T> entities)
        {
            return Ok(await baseService.UpdateBatch(entities));
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体信息</param>
        [HttpDelete("base")]
        [ApiExplorerSettings(GroupName = "v2")]
        public async Task<IActionResult> Delete(T entity)
        {
            return Ok(await baseService.Delete(entity));
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entities">实体信息结合</param>
        [HttpDelete("batch")]
        [ApiExplorerSettings(GroupName = "v2")]
        public async Task<IActionResult> DeleteBatch(List<T> entities)
        {
            return Ok(await baseService.DeleteBatch(entities));
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        [HttpGet("base")]
        [ApiExplorerSettings(GroupName = "v2")]
        public async Task<IActionResult> GetAll()
        {
            return Ok(await baseService.GetAll());
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageSize">每页显示数据条数</param>
        /// <param name="pageIndex">当前页页码</param>
        /// <returns></returns>
        [HttpGet("basepage")]
        [ApiExplorerSettings(GroupName = "v2")]
        public async Task<IActionResult> GetAll(int pageSize, int pageIndex)
        {
            return Ok(await baseService.GetAll(pageSize, pageIndex));
        }
        #endregion

         
        [HttpDelete("soft")]
        [ApiExplorerSettings(GroupName = "v2")]
        public async Task<IActionResult> SoftDelete()
        {
            //获取第一条数据
            var m = (await baseService.GetAll()).FirstOrDefault();

            m.GetType().GetProperty("IsDeleted").SetValue(m, true);

            int h= await baseService.Update(m);

            return Ok(h);
        }
        /// <summary>
        /// 真实查询
        /// </summary>
        /// <returns></returns>
        [HttpGet("realQuery")]
        [ApiExplorerSettings(GroupName = "v2")]
        public async Task<IActionResult> Query()
        {
            var list = (await baseService.GetAll()).ToList();

            return Ok(list.Where(m=>(bool)m.GetType().GetProperty("IsDeleted").GetValue(m)==false));
        }
        ///// <summary>
        ///// 判断数据是否合理
        ///// </summary>
        ///// <typeparam name="TEntity">待验证的实体类型</typeparam>
        ///// <param name="entity">实体信息</param>
        ///// <returns></returns>
        //[NonAction]
        //public ResultDTO<int> JudgeObj<TEntity>(TEntity entity)
        //{

        //}
    }
}
